/*******************************************************************************
 * Copyright (C) 2020, Huada Semiconductor Co., Ltd. All rights reserved.
 *
 * This software component is licensed by HDSC under BSD 3-Clause license
 * (the "License"); You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at:
 *                    opensource.org/licenses/BSD-3-Clause
 */
/******************************************************************************/
/** \file main.c
 **
 ** \brief This sample demonstrates how to set GPIO as output function.
 **
 **   - 2018-10-14  1.0  zhangxl first version for Device Driver Library of GPIO.
 **
 ******************************************************************************/

/*******************************************************************************
 * Include files
 ******************************************************************************/
#include "hc32_ddl.h"
#include "modem.h"

/*******************************************************************************
 * Local type definitions ('typedef')
 ******************************************************************************/

/*******************************************************************************
 * Local pre-processor symbols/macros ('#define')
 ******************************************************************************/

/*******************************************************************************
 * Global variable definitions (declared in header file with 'extern')
 ******************************************************************************/

/*******************************************************************************
 * Local function prototypes ('static')
 ******************************************************************************/

/*******************************************************************************
 * Local variable definitions ('static')
 ******************************************************************************/
static uint8_t u8ExtIntFlag = 1U, u8LedState = 0U;
static uint16_t u16TickValue = 0U;

/*******************************************************************************
 * Function implementation - global ('extern') and local ('static')
 ******************************************************************************/
/**
 *******************************************************************************
 ** \brief SysTick interrupt callback function
 **
 ** \param  None
 **
 ** \retval None
 **
 ******************************************************************************/
void SysTick_IrqHandler(void)
{
    SysTick_IncTick();
    if (0U != u8ExtIntFlag)
    {
        if (++u16TickValue >= 100U)
        {
            u16TickValue = 0U;
            switch (++u8LedState)
            {
                case 1U: BSP_LED_Toggle(LED_RED); break;
                case 2U: BSP_LED_Toggle(LED_GREEN); break;
                case 3U: BSP_LED_Toggle(LED_YELLOW); break;
                case 4U: BSP_LED_Toggle(LED_BLUE); break;
                default: u8LedState = 0U; break;
            }
        }
    }
}

/**
 *******************************************************************************
 ** \brief ExtInt callback function
 **
 ** \param  None
 **
 ** \retval None
 **
 ******************************************************************************/
void BSP_KEY_KEY2_IrqHandler(void)
{
    u8ExtIntFlag = !u8ExtIntFlag;
    if (0u == u8ExtIntFlag)
    {
        u16TickValue = 0U;
        u8LedState = 0U;
        BSP_LED_Off(LED_RED | LED_GREEN | LED_YELLOW | LED_BLUE);
    }
    EXINT_IrqFlgClr(BSP_KEY_KEY2_EXINT);
}


/**
 *******************************************************************************
 ** \brief CRC configure
 **
 ** \param [in] None
 **
 ** \retval None
 **
 ******************************************************************************/
void CRC_Config(void)
{
    /* Enable CRC Clock. */
    PWC_Fcg0PeriphClockCmd(PWC_FCG0_PERIPH_CRC, Enable);
    CRC_Init(CRC_SEL_16B | CRC_REFIN_ENABLE | CRC_REFOUT_ENABLE | CRC_XOROUT_ENABLE);
}

/**
 *******************************************************************************
 ** \brief USART configure
 **
 ** \param [in] None
 **
 ** \retval None
 **
 ******************************************************************************/
void USART_Config(void)
{
    const stc_usart_uart_init_t stcInitCfg =
    {
        UsartIntClkCkNoOutput,
        UsartClkDiv_1,
        UsartDataBits8,
        UsartDataLsbFirst,
        UsartOneStopBit,
        UsartParityNone,
        UsartSampleBit8,
        UsartStartBitFallEdge,
        UsartRtsEnable,
    };

    /* Initialize USART IO */
    PORT_SetFunc(USART_RX_PORT, USART_RX_PIN, USART_RX_FUNC, Disable);
    PORT_SetFunc(USART_TX_PORT, USART_TX_PIN, USART_TX_FUNC, Disable);

    /* Enable peripheral clock */
    PWC_Fcg1PeriphClockCmd(USART_UNIT_CLOCK, Enable);
    /* Initialize USART */
    USART_UART_Init(USART_UNIT, &stcInitCfg);
    /* Set baudrate */
    USART_SetBaudrate(USART_UNIT, USART_BAUDRATE);

    /*Enable RX && TX function*/
    USART_FuncCmd(USART_UNIT, UsartRx, Enable);
    USART_FuncCmd(USART_UNIT, UsartTx, Enable);
}

/**
 *******************************************************************************
 ** \brief  Main function of GPIO output
 **
 ** \param  None
 **
 ** \retval int32_t Return value, if needed
 **
 ******************************************************************************/
int32_t main(void)
{
    /* Configure BSP */
    BSP_CLK_Init();
    BSP_LED_Init();
    BSP_KEY_Init();
    /* Peripheral Init */
    USART_Config();
    CRC_Config();
    /* SysTick configuration */
    SysTick_Init(1000u);

    while(1)
    {
        Modem_Process(1000);
    };
}

/*******************************************************************************
 * EOF (not truncated)
 ******************************************************************************/
